package springcloud.service.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import springcloud.service.model.User;
import springcloud.service.service.UserService;

import java.util.List;

/**
 * 服务接口6013：像 eureka 注册所提供的服务
 */
@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {

    @Value("${source}")
    private String source;
    @Value("${name}")
    private String name;

    @Autowired
    private UserService userService;

    @ResponseBody
    @PostMapping("/addUser")
    public int addUser(@RequestBody User user){ // 对象参数，参数前面需要加上 @RequestBody 注解，不然赋值失败
        log.info("===============${name}=" + name);
        log.info("===============${source}=" + source);
        log.debug("===============addUser");
        return userService.insertSelective(user);
    }

    @ResponseBody
    @GetMapping("/getUserById/{userId}")
    public User getUserById(@PathVariable("userId") Integer userId){
        log.info("===============${name}=" + name);
        log.info("===============${source}=" + source);
        log.debug("===============getUserById");
        return userService.selectByPrimaryKey(userId);
    }

    @ResponseBody
    @GetMapping("/getAll")
    public List<User> getAll(){
        log.info("===============${name}=" + name);
        log.info("===============${source}=" + source);
        log.debug("===============getAll");
        return userService.getAll();
    }
}
